home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / Tools / freeWAIS-sf-1.1 / regexp / try.c < prev   
Encoding:
C/C++ Source or Header  |  1994-08-04  |  5.4 KB  |  250 lines

  1. /*
  2.  * Simple test program for regexp(3) stuff.  Knows about debugging hooks.
  3.  *
  4.  *    Copyright (c) 1986 by University of Toronto.
  5.  *    Written by Henry Spencer.  Not derived from licensed software.
  6.  *
  7.  *    Permission is granted to anyone to use this software for any
  8.  *    purpose on any computer system, and to redistribute it freely,
  9.  *    subject to the following restrictions:
  10.  *
  11.  *    1. The author is not responsible for the consequences of use of
  12.  *        this software, no matter how awful, even if they arise
  13.  *        from defects in it.
  14.  *
  15.  *    2. The origin of this software must not be misrepresented, either
  16.  *        by explicit claim or by omission.
  17.  *
  18.  *    3. Altered versions must be plainly marked as such, and must not
  19.  *        be misrepresented as being the original software.
  20.  *
  21.  * Usage: try re [string [output [-]]]
  22.  * The re is compiled and dumped, regexeced against the string, the result
  23.  * is applied to output using regsub().  The - triggers a running narrative
  24.  * from regexec().  Dumping and narrative don't happen unless DEBUG.
  25.  *
  26.  * If there are no arguments, stdin is assumed to be a stream of lines with
  27.  * five fields:  a r.e., a string to match it against, a result code, a
  28.  * source string for regsub, and the proper result.  Result codes are 'c'
  29.  * for compile failure, 'y' for match success, 'n' for match failure.
  30.  * Field separator is tab.
  31.  */
  32. #include <stdio.h>
  33. #include <regexp.h>
  34.  
  35. #ifdef ERRAVAIL
  36. char *progname;
  37. extern char *mkprogname();
  38. #endif
  39.  
  40. #ifdef DEBUG
  41. extern int regnarrate;
  42. #endif
  43.  
  44. char buf[BUFSIZ];
  45.  
  46. int errreport = 0;        /* Report errors via errseen? */
  47. char *errseen = NULL;        /* Error message. */
  48. int status = 0;            /* Exit status. */
  49.  
  50. /* ARGSUSED */
  51. main(argc, argv)
  52. int argc;
  53. char *argv[];
  54. {
  55.     regexp *r;
  56.     int i;
  57.  
  58. #ifdef ERRAVAIL
  59.     progname = mkprogname(argv[0]);
  60. #endif
  61.  
  62.     if (argc == 1) {
  63.         multiple();
  64.         exit(status);
  65.     }
  66.  
  67.     r = regcomp(argv[1]);
  68.     if (r == NULL)
  69.         error("regcomp failure", "");
  70. #ifdef DEBUG
  71.     regdump(r);
  72.     if (argc > 4)
  73.         regnarrate++;
  74. #endif
  75.     if (argc > 5) {
  76.       char buf[100];
  77.       fgets(buf,100,stdin);
  78.       do {
  79.         i = regexec(r, buf);
  80.         printf("%d %d %s", i, strlen(buf), buf);
  81.         fgets(buf,100,stdin);
  82.       }
  83.       while (!feof(stdin));
  84.       exit(status);
  85.     }
  86.     if (argc > 2) {
  87.         i = regexec(r, argv[2]);
  88.         printf("%d", i);
  89.         for (i = 1; i < NSUBEXP; i++)
  90.             if (r->startp[i] != NULL && r->endp[i] != NULL)
  91.                 printf(" \\%d", i);
  92.         printf("\n");
  93.     }
  94.     if (argc > 3) {
  95.         regsub(r, argv[3], buf);
  96.         printf("%s\n", buf);
  97.     }
  98.     exit(status);
  99. }
  100.  
  101. void
  102. regerror(s)
  103. char *s;
  104. {
  105.     if (errreport)
  106.         errseen = s;
  107.     else
  108.         error(s, "");
  109. }
  110.  
  111. #ifndef ERRAVAIL
  112. error(s1, s2)
  113. char *s1;
  114. char *s2;
  115. {
  116.     fprintf(stderr, "regexp: ");
  117.     fprintf(stderr, s1, s2);
  118.     fprintf(stderr, "\n");
  119.     exit(1);
  120. }
  121. #endif
  122.  
  123. int lineno;
  124.  
  125. regexp badregexp;        /* Implicit init to 0. */
  126.  
  127. multiple()
  128. {
  129.     char rbuf[BUFSIZ];
  130.     char *field[5];
  131.     char *scan;
  132.     int i;
  133.     regexp *r;
  134.     extern char *strchr();
  135.  
  136.     errreport = 1;
  137.     lineno = 0;
  138.     while (fgets(rbuf, sizeof(rbuf), stdin) != NULL) {
  139.         rbuf[strlen(rbuf)-1] = '\0';    /* Dispense with \n. */
  140.         lineno++;
  141.         scan = rbuf;
  142.         for (i = 0; i < 5; i++) {
  143.             field[i] = scan;
  144.             if (field[i] == NULL) {
  145.                 complain("bad testfile format", "");
  146.                 exit(1);
  147.             }
  148.             scan = strchr(scan, '\t');
  149.             if (scan != NULL)
  150.                 *scan++ = '\0';
  151.         }
  152.         try(field);
  153.     }
  154.  
  155.     /* And finish up with some internal testing... */
  156.     lineno = 9990;
  157.     errseen = NULL;
  158.     if (regcomp((char *)NULL) != NULL || errseen == NULL)
  159.         complain("regcomp(NULL) doesn't complain", "");
  160.     lineno = 9991;
  161.     errseen = NULL;
  162.     if (regexec((regexp *)NULL, "foo") || errseen == NULL)
  163.         complain("regexec(NULL, ...) doesn't complain", "");
  164.     lineno = 9992;
  165.     r = regcomp("foo");
  166.     if (r == NULL) {
  167.         complain("regcomp(\"foo\") fails", "");
  168.         return;
  169.     }
  170.     lineno = 9993;
  171.     errseen = NULL;
  172.     if (regexec(r, (char *)NULL) || errseen == NULL)
  173.         complain("regexec(..., NULL) doesn't complain", "");
  174.     lineno = 9994;
  175.     errseen = NULL;
  176.     regsub((regexp *)NULL, "foo", rbuf);
  177.     if (errseen == NULL)
  178.         complain("regsub(NULL, ..., ...) doesn't complain", "");
  179.     lineno = 9995;
  180.     errseen = NULL;
  181.     regsub(r, (char *)NULL, rbuf);
  182.     if (errseen == NULL)
  183.         complain("regsub(..., NULL, ...) doesn't complain", "");
  184.     lineno = 9996;
  185.     errseen = NULL;
  186.     regsub(r, "foo", (char *)NULL);
  187.     if (errseen == NULL)
  188.         complain("regsub(..., ..., NULL) doesn't complain", "");
  189.     lineno = 9997;
  190.     errseen = NULL;
  191.     if (regexec(&badregexp, "foo") || errseen == NULL)
  192.         complain("regexec(nonsense, ...) doesn't complain", "");
  193.     lineno = 9998;
  194.     errseen = NULL;
  195.     regsub(&badregexp, "foo", rbuf);
  196.     if (errseen == NULL)
  197.         complain("regsub(nonsense, ..., ...) doesn't complain", "");
  198. }
  199.  
  200. try(fields)
  201. char **fields;
  202. {
  203.     regexp *r;
  204.     char dbuf[BUFSIZ];
  205.  
  206.     errseen = NULL;
  207.     r = regcomp(fields[0]);
  208.     if (r == NULL) {
  209.         if (*fields[2] != 'c')
  210.             complain("regcomp failure in `%s'", fields[0]);
  211.         return;
  212.     }
  213.     if (*fields[2] == 'c') {
  214.         complain("unexpected regcomp success in `%s'", fields[0]);
  215.         free((char *)r);
  216.         return;
  217.     }
  218.     if (!regexec(r, fields[1])) {
  219.         if (*fields[2] != 'n')
  220.             complain("regexec failure in `%s'", "");
  221.         free((char *)r);
  222.         return;
  223.     }
  224.     if (*fields[2] == 'n') {
  225.         complain("unexpected regexec success", "");
  226.         free((char *)r);
  227.         return;
  228.     }
  229.     errseen = NULL;
  230.     regsub(r, fields[3], dbuf);
  231.     if (errseen != NULL) {
  232.         complain("regsub complaint", "");
  233.         free((char *)r);
  234.         return;
  235.     }
  236.     if (strcmp(dbuf, fields[4]) != 0)
  237.         complain("regsub result `%s' wrong", dbuf);
  238.     free((char *)r);
  239. }
  240.  
  241. complain(s1, s2)
  242. char *s1;
  243. char *s2;
  244. {
  245.     fprintf(stderr, "try: %d: ", lineno);
  246.     fprintf(stderr, s1, s2);
  247.     fprintf(stderr, " (%s)\n", (errseen != NULL) ? errseen : "");
  248.     status = 1;
  249. }
  250.